package com.hdu.clj.fitness

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.core.text.isDigitsOnly
import kotlinx.android.synthetic.main.activity_plan.*
import java.io.*

class PlanActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_plan)
        ActivityCollector.addActivity(this)
        //用户总数据表
        val dbHelper = userDatabase(this,"Users.db",1)
        val db = dbHelper.writableDatabase
        val curUserName = load("userName")
        val curUserAcc = load("userAcc")

        buttonDayPlan.setOnClickListener {
            if (editDayPlan.text.isNotEmpty()){
                if (editDayPlan.text.isDigitsOnly()) {
                    if (editDayPlan.text.toString().length < 9) {
                        val dayNum = editDayPlan.text.toString()
                        db.execSQL(
                            "update Users set dayNum = ? where acc = ?",
                            arrayOf(dayNum, curUserAcc)
                        )
                        Toast.makeText(this, "每日计划设置成功！", Toast.LENGTH_SHORT).show()
                    } else {
                        Toast.makeText(this, "你疯了吧，请重新设置！", Toast.LENGTH_SHORT).show()
                        editDayPlan.text.clear()
                    }
                }else{
                    Toast.makeText(this, "请输入数字！", Toast.LENGTH_SHORT).show()
                }
            }
            else{
                Toast.makeText(this,"请输入数据！",Toast.LENGTH_SHORT).show()
            }
            editDayPlan.text.clear()
        }
        buttonTotalPlan.setOnClickListener {
            if (editTotalPlan.text.isNotEmpty()){
                if (editTotalPlan.text.isDigitsOnly()) {
                    if (editTotalPlan.text.toString().length < 9) {
                        val totalNum = editTotalPlan.text.toString()
                        db.execSQL(
                            "update Users set totalNum = ? where acc = ?",
                            arrayOf(totalNum, curUserAcc)
                        )
                        Toast.makeText(this, "总计划设置成功！", Toast.LENGTH_SHORT).show()
                    } else {
                        Toast.makeText(this, "你确定？还是重新设置吧！", Toast.LENGTH_SHORT).show()
                        editTotalPlan.text.clear()
                    }
                }else{
                    Toast.makeText(this, "请输入数字！", Toast.LENGTH_SHORT).show()
                }
            }
            else{
                Toast.makeText(this,"请输入数据！",Toast.LENGTH_SHORT).show()
            }
            editTotalPlan.text.clear()
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this)
    }

    private fun load(data:String): String {
        val content = StringBuilder()

        try {
            val input = openFileInput(data)
            //val reader = BufferedReader(InputStreamReader(input))
            val reader = BufferedReader(InputStreamReader(input))
            reader.use {
                reader.forEachLine {
                    content.append(it)
                }
            }
        }catch (e:IOException){
            e.printStackTrace()
        }
        return content.toString()
    }
}
